package com.dome.controller;

import com.dome.annotation.Idempotence;
import com.dome.common.utils.IpUtils;
import com.dome.common.result.JsonResult;
import com.dome.common.utils.RedisUtils;
import com.dome.serice.asyncapi.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @param
 * @author tangshunkai
 */
@Slf4j
@RestController
public class TestController {

    @Autowired
    private RedisUtils redisUtil;

    @Autowired
    private AsyncService asyncService;


    @RequestMapping("getToken")
    public JsonResult getToken(HttpServletRequest request){
        String token = UUID.randomUUID().toString();
        request.setAttribute("token", token);
        log.info("获取token：{},数字是：{}",token,1234);
        redisUtil.set(token,"接口幂等性",5);
        return JsonResult.success(token);
    }

    @Idempotence // 自定义注解
    @RequestMapping("testDome")
    public JsonResult testDome(HttpServletRequest request){

        return JsonResult.success();
    }

    @RequestMapping("testToken")
    public JsonResult testToken(HttpServletRequest request){
        String token = request.getHeader("token");
        redisUtil.set(token,"修改数据",5);
        System.out.println(redisUtil.get(token));
        return JsonResult.success();
    }

    @RequestMapping("setRoomId")
    public JsonResult setRoomId(String sysId,String roomId){
        redisUtil.sSet(roomId,sysId);
        return JsonResult.success();
    }

    @RequestMapping("getRoomId")
    public JsonResult getRoomId(String roomId){
        long l = redisUtil.sGetSetSize(roomId);
        return JsonResult.success(l);
    }

//    @Idempotence
    @RequestMapping("getProvince")
    public JsonResult getProvince(HttpServletRequest request){
//        String ipUrl="http://ip-api.com/json/183.17.228.2";
//        String forObject1 = restTemplate.getForObject(ipUrl, String.class);
//        JSONObject jsonObject = JSONObject.parseObject(forObject1);
        String ipAddr = IpUtils.getIpAddr(request);
        Cookie[] cookies = request.getCookies();
        log.info("打印ip：{},打印，cookies：{}",ipAddr,cookies);
        return JsonResult.success(ipAddr);
    }


    @RequestMapping("asyncTest")
    public void asyncTest(){
        log.info("主线程执行结果，"+Thread.currentThread().getName());
        asyncService.asyncTest();
        log.info("执行结束");
    }
}
